<?php

/*
+--------------------------------------------------------------------------
|   Invision Power Board
|   =============================================
|   by Matthew Mecham
|   (c) 2001 - 2006 Invision Power Services, Inc.
|   
|   =============================================
|   
|   
+---------------------------------------------------------------------------
|   > $Date: 2007-05-23 11:18:18 -0400 (Wed, 23 May 2007) $
|   > $Revision: 1005 $
|   > $Author: bfarber $
+---------------------------------------------------------------------------
|
|   > Posting Editor
|   > Module written by Matt Mecham
|   > Date started: Thursday 10th March 2005 11:08
|
+--------------------------------------------------------------------------
*/

/**
* Text Editor Main Class
*
* This class contains all the main class functions
* EXAMPLE USAGE
* <code>
* $han_editor           = new han_editor();
* $han_editor->ipsclass =& $this->ipsclass;
* $han_editor->init();
* 
* # To generate the HTML for the editor:
* $editor_html = $han_editor->show_editor( $raw_post, 'Post' );
* 
* # $raw_post is either raw HTML for the rich text editor or raw BBCode tagged text when
* #  using the standard editor. (See the document on the BBCode parser for more information)
* # 'Post' is the name of the form field you wish use. In this case it'll return $_POST['Post'].
* 
* #To process the editor's text into tagged BBCode text:
* $post = $han_editor->process_raw_post( 'Post' );
* 
* #'Post' is the name of the form field you used when displaying the editor. As in $_POST['Post']
* 
* # To use the HTML generated by editor class for displaying the actual editor (in our example; $editor_html)
* # you'll need to use it like this:
* 
* <form id='postingform' onsubmit='return ValidateForm()' action="$url" method="post" name="REPLIER">
* {$editor_html}
* <br /><input type="submit" value="Post" /></div>
* </form>
* 
* The form tag MUST have: id='postingform' onsubmit='return ValidateForm()' otherwise the editor will fail.
* </code>
*
* @package		InvisionPowerBoard
* @subpackage	TextEditor
* @author		Matt Mecham
* @copyright	Invision Power Services, Inc.
* @version		2.1
*/

/**
*
*/

/**
* Text Editor Main Class
*
* Main object class
*
* @package		InvisionPowerBoard
* @subpackage	TextEditor
* @author  	 	Matt Mecham
* @version		2.1
* @since		2.1.0
*/

if ( ! defined( 'IN_IPB' ) )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded all the relevant files.";
	exit();
}

class class_editor
{
	/**
	* Main IPS class object
	*
	* @var	object
	*/
	var $ipsclass;
	
	/**
	* Allow unicode setting
	*
	* @var	integer
	*/
	var $allow_unicode;
	
	/**
	* Get magic quotes
	*
	* @var	integer
	*/
	var $get_magic_quotes;
	
	/**
	* Reverse font sizes
	*
	* @var	array
	*/
	var $rev_font_sizes = array();
	
	/**
	* Main font sizes
	*
	* @var	array
	*/
	var $font_sizes     = array( 1 => 8,
								 2 => 10,
								 3 => 12,
								 4 => 14,
								 5 => 18,
								 6 => 24,
								 7 => 36 );
	
	/*-------------------------------------------------------------------------*/
	// Main init
	/*-------------------------------------------------------------------------*/
	
	/**
	* Main init: Prep font sizes
	*
	* @return	void
	*/
	function main_init(  )
	{
		//-------------------------------
		// Remap font sizes
		//-------------------------------
		
		foreach( $this->font_sizes as $bbcode => $real )
		{
			$this->rev_font_sizes[ $real ] = $bbcode;
		}
	}
	
	/*-------------------------------------------------------------------------*/
	// Get real font size
	/*-------------------------------------------------------------------------*/
	
	/**
	* Get BBCode font size from real PX size
	*
	* @param	integer	PX Size
	* @return	integer	BBCode size
	*/
	function convert_realsize_to_bbsize( $real )
	{
		$real = intval( $real );
		
		if ( $this->rev_font_sizes[ $real ] )
		{
			return $this->rev_font_sizes[ $real ];
		}
		else
		{
			return 3;
		}
	}
	
	/*-------------------------------------------------------------------------*/
	// unhtml smilie emoid: Replaces < and > to ascii as < is &lt; upon smilie
	// save from ACP, so we don't want to use that.
	/*-------------------------------------------------------------------------*/
	
	/**
	* unhtml smilie emoid: Replaces < and > to ascii as < is &lt; upon smilie
	* save from ACP, so we don't want to use that.
	*
	* @param	string	Raw text
	* @return	string	Converted text
	*/
	function unhtml_emoid( $matches=array() )
	{
		$emoid  = stripslashes( $matches[2] );
		
		$emoid  = str_replace( '<', '&#60;', $emoid );
		$emoid  = str_replace( '>', '&#62;', $emoid );
		
		return 'emoid="' . $emoid . '"';
	}
	
	/*-------------------------------------------------------------------------*/
	// html smilie emoid: Opposite of unhtml_emoid
	/*-------------------------------------------------------------------------*/
	
	/**
	* html smilie emoid: Opposite of unhtml_emoid
	*
	* @param	string	Raw text
	* @return	string	Converted text
	*/
	function html_emoid( $matches=array() )
	{
		$emoid  = stripslashes( $matches[2] );
		
		$emoid  = str_replace( '&#60;', '<', $emoid );
		$emoid  = str_replace( '&#62;', '>', $emoid );
		$emoid  = str_replace( '&quot;', '-', $emoid );
		
		return ' ' . $emoid . ' ';
	}
	
	/*-------------------------------------------------------------------------*/
	// unhtml url: Removes < and >
	/*-------------------------------------------------------------------------*/
	
	/**
	* unhtml url: Removes < and >
	*
	* @param	string	Type (IMG / A HREF)
	* @param	string	URL
	* @return	string	Converted text
	*/
	function unhtml_url( $matches=array() )
	{
		$url  = stripslashes( $matches[3] );
		$type = stripslashes( $matches[1] ? $matches[1] : 'a href' );
		
		$url  = str_replace( '<', '&lt;', $url );
		$url  = str_replace( '>', '&gt;', $url );
		$url  = str_replace( ' ', '%20' , $url );
		
		return '<'.$type.'="'.$url.'"';
	}
	
	/*-------------------------------------------------------------------------*/
	// make_rtf_safe: Formats HTML to make safe for preloading RTE
	/*-------------------------------------------------------------------------*/
	
	/**
	* Converts color:rgb(x,x,x) to color:#xxxxxx
	*
	* @param	string	rgb contents: x,x,x
	* @param	string	regex end
	* @return	string	Converted text
	*/
	function _rgb_to_hex($matches)
	{
		$t  = $matches[1];
		$t2 = $matches[2];
		
		$tmp = array_map( "trim", explode( ",", $t ) );
		return 'color: ' . sprintf("#%02X%02X%02X".$t2, intval($tmp[0]), intval($tmp[1]), intval($tmp[2]) );
	}
	
	/*-------------------------------------------------------------------------*/
	// make_rtf_safe: Formats HTML to make safe for preloading RTE
	/*-------------------------------------------------------------------------*/
	
	/**
	* make_rtf_safe: Formats HTML to make safe for preloading RTE
	*
	* @param	string	Raw text
	* @return	string	Converted text
	*/
	function _make_rtf_safe($t)
	{
		$t = trim($t);
						
		//-------------------------------
		// Convert all types of single quotes
		//-------------------------------
		
		if ( strtolower($this->ipsclass->vars['gb_char_set']) != 'utf-8' )
		{
			$t = str_replace(chr(145), chr(39), $t);
			$t = str_replace(chr(146), chr(39), $t);
		}
		
		$t = str_replace("'"     , "&#39;", $t);
		
		//-------------------------------
		// Convert all types of double quotes
		//-------------------------------
		
		if ( strtolower($this->ipsclass->vars['gb_char_set']) != 'utf-8' )
		{		
			$t = str_replace(chr(147), chr(34), $t);
			$t = str_replace(chr(148), chr(34), $t);
		}
		
		//-------------------------------
		// Replace carriage returns & line feeds
		//-------------------------------
		
		$t = str_replace(chr(10), " ", $t);
		$t = str_replace(chr(13), " ", $t);
		
		return $t;
	}
	
	/*-------------------------------------------------------------------------*/
	// Clean up IPB html
	/*-------------------------------------------------------------------------*/
	
	/**
	* Clean up IPB html
	*
	* @param	string	Raw text
	* @return	string	Converted text
	*/
	function _clean_ipb_html( $t="" )
	{
		//-----------------------------------------
		// Remove all comments
		//-----------------------------------------
		
		$t = preg_replace( "#\<\!\-\-(.+?)\-\-\>#is", "", $t );
		$t = str_replace( '&#39;'  , "'", $t );
		$t = str_replace( '&#039;' , "'", $t );
		$t = str_replace( '&#33;'  , "!", $t );
		
		//-----------------------------------------
		// left, right, center
		//-----------------------------------------
		
		$t = preg_replace( "#\[(left|right|center)\](.+?)\[/\\1\]#is"  , "<div align=\"\\1\">\\2</div>", $t );
		
		//-----------------------------------------
		// Indent => Block quote
		//-----------------------------------------
		
		while( preg_match( "#\[indent\](.+?)\[/indent\]#is" , $t ) )
		{
			$t = preg_replace( "#\[indent\](.+?)\[/indent\]#is"  , "<blockquote>\\1</blockquote>", $t );
		}
			
		return $t;
	}
	
	/*-------------------------------------------------------------------------*/
	// Clean up and make the post safe for the DB
	/*-------------------------------------------------------------------------*/
	
	/**
	* Clean up and make the post safe for the DB
	*
	* @param	string	Raw text
	* @return	string	Converted text
	*/
	function _clean_post( $t )
    {
    	if ( $t == "" )
    	{
    		return "";
    	}
    	
    	//-----------------------------------------
    	// Make it safe
    	//-----------------------------------------
    	
    	$t = str_replace( "&"            , "&amp;"         , $t );
    	$t = str_replace( "<!--"         , "&#60;&#33;--"  , $t );
    	$t = str_replace( "-->"          , "--&#62;"       , $t );
    	$t = preg_replace( "/<script/i"  , "&#60;script"   , $t );
    	$t = str_replace( ">"            , "&gt;"          , $t );
    	$t = str_replace( "<"            , "&lt;"          , $t );
    	$t = str_replace( '"'            , "&quot;"        , $t );
    	$t = preg_replace( "/\n/"        , "<br />"        , $t );
    	$t = preg_replace( "/\\\$/"      , "&#036;"        , $t );
    	$t = preg_replace( "/\r/"        , ""              , $t );
    	$t = str_replace( "!"            , "&#33;"         , $t );
    	$t = str_replace( "'"            , "&#39;"         , $t );
    	
    	//-----------------------------------------
    	// Ensure unicode chars are OK
    	//-----------------------------------------
    	
    	if ( $this->allow_unicode )
		{
			$t = preg_replace("/&amp;#([0-9]+);/s", "&#\\1;", $t );
		}
		
		//-----------------------------------------
		// Strip slashes if not already done so.
		//-----------------------------------------
		
    	if ( $this->get_magic_quotes )
    	{
    		$t = stripslashes($t);
    	}
    	
    	//-----------------------------------------
    	// Swap user inputted backslashes
    	//-----------------------------------------
    	
    	$t = preg_replace( "/\\\(?!&amp;#|\?#)/", "&#092;", $t ); 
    	
    	return $t;
    }
	
}


?>